package top.goldenyear.porpoise.framework.web.core.generator.config;

import cn.hutool.v7.core.date.TimeUtil;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * 全局配置
 *
 * @author airhead
 */
@Data
@Accessors(chain = true)
public class GlobalConfig {
  private String author = System.getProperty("user.name");
  private Supplier<String> since = () -> TimeUtil.format(LocalDateTime.now(), "yyyy-MM-dd");
  private Boolean overwrite = false;

  private PackageConfig packageConfig;
  private UiConfig uiConfig;
  private StrategyConfig strategyConfig;

  private Map<String, ModuleConfig> moduleConfigs;
  private Map<String, Boolean> generateConfigs;

  public GlobalConfig() {
    packageConfig = new PackageConfig();
    uiConfig = new UiConfig();
    strategyConfig = new StrategyConfig();
    moduleConfigs = new HashMap<>();
    generateConfigs = new HashMap<>();
  }

  public static GlobalConfig of() {
    return new GlobalConfig();
  }

  public String getSince() {
    return since.get();
  }

  public GlobalConfig setSince(String since) {
    this.since = () -> since;
    return this;
  }

  public GlobalConfig setSince(Supplier<String> since) {
    this.since = since;
    return this;
  }

  public GlobalConfig addModuleConfig(ModuleConfig moduleConfig) {
    moduleConfigs.put(moduleConfig.getModule(), moduleConfig);
    return this;
  }

  public GlobalConfig setSourceDir(String sourceDir) {
    packageConfig.setSourceDir(sourceDir);
    return this;
  }

  public GlobalConfig setUiSourceDir(String sourceDir) {
    uiConfig.setSourceDir(sourceDir);
    return this;
  }

  public GlobalConfig setBasePackage(String basePackage) {
    packageConfig.setBasePackage(basePackage);
    return this;
  }

  public GlobalConfig setBiz(String biz) {
    packageConfig.setBiz(biz);
    return this;
  }


  public GlobalConfig addDomainConfig(DomainConfig domainConfig) {
    strategyConfig.addDomainConfig(domainConfig);
    return this;
  }

  public GlobalConfig addGenerateConfig(String module, Boolean generate) {
    generateConfigs.put(module, generate);
    return this;
  }
}
